/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lmx.common.constants.CommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.CustomerService;
import com.lmx.persistence.service.WorkPlaceService;
import com.lmx.repoDomain.tmodel.Bed;
import com.lmx.repoDomain.tmodel.Customer;
import com.lmx.repoDomain.tmodel.CustomerServerChange;

import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

@JsonApiResourceRepository(CustomerServerChange.class)
@Component
public class CustomerServerChangeRepository extends BaseRepository<CustomerServerChange> {
	
	@Autowired
	private CustomerService customerService;

	@Autowired
	private WorkPlaceService workPlaceService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(CustomerServerChange.class);
	}

	@Override
	@JsonApiSave
	public CustomerServerChange save(CustomerServerChange record) {
		
		if (record.getId() == null) {
			dslJpaService.create(record);
			if (record.getBedOld() != null && record.getBedNew() != null) {
				if (record.getBedOld().getId().equals(record.getBedNew().getId())) {// 床位为更改

				} else {
					// 床位发生更改 将新的床位设置为已入住
					workPlaceService.changeDedStauts(record.getBedNew(),
							commonService.getDicttype(CommonConstants.DICTTYPE.BED_STATUS_2.label()));
				}
			}
		} else {
			if (record.getDelStatus() == 1) {// 删除
				record = customerService.deleteCustomerServerChange(record);
				return record;
			} else {
				CustomerServerChange recordOld = customerService.getCustomerServerChange(record);
				if (!"applyStatus2".equals(recordOld.getStatus().getTypecode())) {// applyStatus2已生效
					Bed temp1 = recordOld.getBedNew();
					Bed temp2 = record.getBedNew();
					if (temp1 != null && !temp1.getId().equals(temp2.getId())) {
						// 先前更改的床位与现在更改的床位不一致将先前修改床位置为空闲
						workPlaceService.changeDedStauts(temp1,
								commonService.getDicttype(CommonConstants.DICTTYPE.BED_STATUS_1.label()));
					}
					if (record.getBedOld() != null && record.getBedNew() != null) {
						if (record.getBedOld().getId().equals(record.getBedNew().getId())) {// 床位未更改

						} else {
							// 床位发生更改 将新的床位设置为已入住
							workPlaceService.changeDedStauts(record.getBedNew(),
									commonService.getDicttype(CommonConstants.DICTTYPE.BED_STATUS_2.label()));
						}
					}
				}
			}
			dslJpaService.update(record);
		}
		
		String curTime = CommonUtil.getCurrTimeForString();				// 当日日期
		String effectiveTime = CommonUtil.integetTimeForString(record.getEffectiveTime()).substring(0, 10);
		// 如果是当天生效的，则直接生效
		if (curTime.substring(0, 10).equals(effectiveTime)) {
			customerService.operateCustomerServerChange(record);
		} else {
			Customer customer = customerService.getCustomer(record.getCustomer());
			customer.setApplyFlag(1);
			dslJpaService.update(customer);
		}
		return record;
	}

}
